﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;

namespace Twittervice.api
{
    /// <summary>
    /// This class represents a user of the Twitter Service
    /// </summary>
    public class TwitterUser
    {
        public const string USER_TAG = "user";

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name">screen name of this user</param>
        /// <param name="pass">password of this user</param>
        public TwitterUser(String name, String pass)
        {
            ScreenName = name;
            password = pass;
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public TwitterUser()
        {
        }

        private string screenName;

        public string ScreenName
        {
            get { return screenName; }
            set { screenName = value; }
        }

        private String password;

        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private long id;

        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        private string location;

        public string Location
        {
            get { return location; }
            set { location = value; }
        }

        private string description;

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        private string profileImage;

        public string ProfileImage
        {
            get { return profileImage; }
            set { profileImage = value; }
        }

        private string userUrl;

        public string UserUrl
        {
            get { return userUrl; }
            set { userUrl = value; }
        }

        private int followersCount;

        public int FollowersCount
        {
            get { return followersCount; }
            set { followersCount = value; }
        }

        private int friendsCount;

        public int FriendsCount
        {
            get { return friendsCount; }
            set { friendsCount = value; }
        }

        private bool following;

        public bool Following
        {
            get { return following; }
            set { following = value; }
        }

        private DateTime createdAt;

        public DateTime CreatedAt
        {
            get { return createdAt; }
            set { createdAt = value; }
        }

        /// <summary>
        /// Parses an XML structure to create a TwitterUser object.
        /// </summary>
        /// <param name="nodeList">List of XML nodes</param>
        /// <returns>The associated TwitterUser object</returns>
        public static TwitterUser parseXml(XmlNodeList nodeList)
        {
            TwitterUser user = new TwitterUser();

            foreach (XmlNode n in nodeList)
            {
                switch (n.Name)
                {
                    case "id":
                        user.Id = long.Parse(n.InnerText);
                        break;

                    case "name":
                        user.Name = n.InnerText;
                        break;

                    case "screen_name":
                        user.screenName = n.InnerText;
                        break;

                    case "location":
                        user.Location = n.InnerText;
                        break;

                    case "description":
                        user.Description = n.InnerText;
                        break;

                    case "profile_image_url":
                        user.profileImage = n.InnerText;
                        break;

                    case "url":
                        user.UserUrl = n.InnerText;
                        break;

                    case "protected":
                        // TBD
                        break;

                    case "followers_count":
                        user.FollowersCount = int.Parse(n.InnerText);
                        break;

                    case "friendsCount":
                        user.FriendsCount = int.Parse(n.InnerText);
                        break;

                    case "following":
                        if (n.InnerText != "")
                        {
                            user.following = bool.Parse(n.InnerText);
                        }
                        break;

                    case "created_at":
                        user.CreatedAt = DateTime.ParseExact(n.InnerText, 
                            "ddd MMM dd HH:mm:ss zzz yyyy", 
                            System.Globalization.CultureInfo.InvariantCulture);
                        break;
                }
            }

            return user;
        }    
    }
}
